//
//  WLDataBaseManager.m
//  WanLian
//
//  Created by 123456 on 2018/12/9.
//  Copyright © 2018 wanlian. All rights reserved.
//

#import "WLDataBaseManager.h"
#define DATABASE_TABLE_NAME @"FileUpDownloadTable"
#define DATABASE_NAME @"FileUpDownloadDataBase"
@interface WLDataBaseManager ()

@end
@implementation WLDataBaseManager
impl_single_instance(WLDataBaseManager)

- (void)configure {
    [self createDB];
}
- (void)createDB {
    /**
     自定义数据库名称，否则默认为BGFMDB
     */
    bg_setSqliteName(DATABASE_NAME);
    
}
- (BOOL)insertTableName:(NSString *)tableName withModel:(NSObject *)model {
    model.bg_tableName = tableName;
    return [model bg_save];
}
- (BOOL)deleteTableName:(NSString *)tableName withClassName:(Class)className conditionDict:(NSDictionary *)dict {
    
    NSString* where = [self getConditionStrWithDict:dict];
    if (where) {
        return [className bg_delete:tableName where:where];
    }
    return NO;
}

- (BOOL)updateTableName:(NSString *)tableName withModel:(NSObject *)model conditionDict:(NSDictionary *)dict {
    model.bg_tableName = tableName;
    NSString *where = [self getConditionStrWithDict:dict];
    if (where) {
        return [model bg_updateWhere:where];
    }
    return NO;
}




- (void)insertAsynTableName:(NSString *)tableName withModel:(NSObject *)model complete:(bg_complete_B)complete {
    model.bg_tableName = tableName;
    [model bg_saveAsync:complete];
}
- (void)deleteAsynTableName:(NSString *)tableName withClassName:(Class)className conditionDict:(NSDictionary *)dict complete:(bg_complete_B)complete {
    
    NSString* where = [self getConditionStrWithDict:dict];
    if (where) {
        [className bg_deleteAsync:tableName where:where complete:complete];
    }
}

- (void)updateAsynTableName:(NSString *)tableName withModel:(NSObject *)model keyPathDict:(NSDictionary *)dict complete:(bg_complete_B)complete {
    model.bg_tableName = tableName;
    NSString *where = [self getConditionStrWithDict:dict];
    if (where) {
        [model bg_updateAsyncWhere:where complete:complete];
    }
}
- (NSArray *)queryWithTableName:(NSString *)tableName className:(Class)className conditionDict:(NSDictionary *)dict {
    NSString *where = [self getConditionStrWithDict:dict];
    if (where) {
        return [className bg_find:tableName where:where];
    }
    return nil;
}
- (NSArray *)queryTableName:(NSString *)tableName withPage:(int)page className:(Class)className orderBy:(NSString* _Nullable)orderBy desc:(BOOL)desc {
    
    NSArray* arr = [className bg_find:tableName range:NSMakeRange(page,10) orderBy:orderBy desc:desc];
    return arr;
}

- (NSArray *)queryClassName:(Class)className tableName:(NSString *)tableName condition:(NSDictionary *)condictionDict fromIndex:(NSInteger)startIndex step:(NSInteger)step orderBy:(NSString* _Nullable)orderBy desc:(BOOL)desc {
    NSString *conditionStr = [self getConditionStrWithDict:condictionDict];
    
    conditionStr = [conditionStr stringByAppendingFormat:@" order by %@ %@ limit %ld,%ld", bg_sqlKey(orderBy), desc ? @"desc":@"asc", startIndex, step];
    
    return [className bg_find:tableName where:conditionStr];
}
- (NSArray *)queryClassName:(Class)className tableName:(NSString *)tableName fromIndex:(NSInteger)startIndex step:(NSInteger)step orderBy:(NSString* _Nullable)orderBy desc:(BOOL)desc {
    NSArray* arr = [className bg_find:tableName range:NSMakeRange(startIndex + 1,step) orderBy:orderBy desc:desc];
    return arr;
}
- (NSArray *)queryTotalListWithTableName:(NSString *)tableName className:(Class)className {
    NSArray *arr = [className bg_findAll:tableName];
    return arr;
}

- (NSString *)getConditionStrWithDict:(NSDictionary *)dict {
    if (dict) {
        NSString *res = nil;
        NSMutableString *muStr = [NSMutableString stringWithString:@"where "];
        for (NSString *key in dict.allKeys) {
            if ([dict[key] isKindOfClass:[NSString class]] && [dict[key] isEqualToString:@"is not null"]) {
                [muStr appendFormat:@"trim(%@) != ''", bg_sqlKey(key)];
//                [muStr appendFormat:@"%@ %@",bg_sqlKey(key), bg_sqlValue(dict[key])];
            } else {
                [muStr appendFormat:@"%@=%@", bg_sqlKey(key), bg_sqlValue(dict[key])];
            }
        }
        if ([muStr hasSuffix:@","]) {
            res = [muStr substringToIndex:muStr.length -1];
        } else {
            res = muStr;
        }
        return res;
    }
    return nil;
    
}
- (NSInteger)countOfTableName:(NSString *)tableName {
    return [NSObject bg_count:tableName where:nil];
}
- (NSInteger)countOfTableName:(NSString *)tableName condictionDict:(NSDictionary *)condict {
    NSString *where = [self getConditionStrWithDict:condict];
    return [NSObject bg_count:tableName where:where];
}
//- (BOOL)updateWithModel:(NSObject *)model {
//    NSString *tableName = model.bg_tableName;
//    return [self alterTableName:tableName withModel:model];
//}
//- (BOOL)deleteWithModel:(NSObject *)model {
//
//    NSString *tableName = model.bg_tableName;
//    return [self deleteTableName:tableName withClassName:[model class] conditionDict:<#(nonnull NSDictionary *)#>:model];
//}
//- (BOOL)insertWithModel:(NSObject *)model {
//    NSString *tableName = model.bg_tableName;
//    return [self insertTableName:tableName withModel:model];
//}
//- (NSArray *)queryTotalListWithClassName:(Class)className tableName:(NSString *)tableName {
//    return [self queryTotalListWithTableName:tableName className:className];
//}
//- (NSArray *)queryFromIndex:(NSInteger)startIndex step:(NSInteger)step withTableName:(NSString *)tableName{
//    return [self queryTableName:tableName fromIndex:startIndex step:step];
//}
@end
